<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$isRealShow = (int)$_data["isRealShow"];
$isSpecial = (int)$_data["isSpecial"];
if(strlen($_data["isSpecial"]) == 0)$isSpecial = 1;

$intGID = (int)$_data["GambleID"];
$strBT = trim($_data["Bet_Type"]);
$arrBType = explode(",", $strBT);

$intSpecialNumType = (int)$_data["SpecialNumType"];
if($strBT == "14")$arrBType = array(11, 12, 13);

for($i = 0; $i < sizeof($arrBType); $i++)include(__Common_Path."/todaygame/TodayGame".$intLottoID."_".$arrBType[$i].".inc.php");

if(!is_array($_arrTodayGame[$intGID])){
	$GLOBALS["errormessage"] = "No TodayGame Data!!";
	ErrMsgOutputHTML("Error", "", 1);
}
$arrTodayGame = $_arrTodayGame[$intGID];
	
$strWagerType = "-1";
$intUserID = $_SESSION["Admin_Info"] -> ID;
switch($arrPower[1]){
	case "all":
		$strSeach = " ";
		$strSeach2 = " ";
		$strMulti = ($isRealShow == 1)?"*(_DirectorRate)":"";
		$strComm = ($isRealShow == 1)?"*(_DirectorCommission)":"*0";
		$strWagerType = "0,1,2,3,4,5";
		$strSWType = "-999";
		break;

	case "director-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "director":
		if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){	//公司總帳
			$strSeach = " ";
			$strSeach2 = " ";
			$strMulti = "*(_DirectorRate)";
			$strComm = "*(_DirectorCommission)";
			$strWagerType = "0,1,2,3,4,5";
			$strSWType = "-999";
		}else{
			//if(empty($intDirectorID))$intDirectorID = $_SESSION["Admin_Info"] -> ID;
			$strSeach = " and DirectorID = ".$intUserID." ";
			$strSeach2 = " and b.DirectorID = ".$intUserID." ";
			$strMulti = ($isRealShow == 1)?"*(_StockRate - _DirectorRate)":"";
			$strComm = ($isRealShow == 1)?"*_StockCommission":"*0";
			$strWagerType = "0,2,3,4,5";
			$strSWType = "5";
		}
		break;

	case "stock-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "stock":
		/*if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){	//總監總帳
			if($_SESSION["Admin_Info"] -> ParentisAllAccount == 1){
				$strSeach = " ";
				$strSeach2 = " ";
				$strMulti = "*(_DirectorRate)";
				$strComm = "*(_DirectorCommission)";
				$strWagerType = "0,1,2,3,4";
				$strSWType = "-999";
			}else{
				$strSeach = " and DirectorID = ".$_SESSION["Admin_Info"] -> ParentID." ";
				$strSeach2 = " and b.DirectorID = ".$_SESSION["Admin_Info"] -> ParentID." ";
				$strMulti = "*(_DirectorRate)";
				$strComm = "*(_DirectorCommission)";
				$strWagerType = "0,1,2,3,4";
				$strSWType = "-999";
			}
		}else{*/
			$strSeach = " and StockID = ".$intUserID." ";
			$strSeach2 = " and b.StockID = ".$intUserID." ";
			$strMulti = ($isRealShow == 1)?"*(_MainRate - _StockRate)":"";
			$strComm = ($isRealShow == 1)?"*_MainCommission":"*0";
			$strWagerType = "0,2,3,4";
			$strSWType = "4";
		//}
		break;

	case "main-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "main":
		$strSeach = " and MainID = ".$intUserID." ";
		$strSeach2 = " and b.MainID = ".$intUserID." ";
		$strMulti = ($isRealShow == 1)?"*(_SubRate - _MainRate)":"";
		$strComm = ($isRealShow == 1)?"*_SubCommission":"*0";
		$strWagerType = "0,2,3";
		$strSWType = "3";
		break;

	case "sub-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "sub";
		$strSeach = " and SubID = ".$intUserID." ";
		$strSeach2 = " and b.SubID = ".$intUserID." ";
		$strMulti = ($isRealShow == 1)?"*(1 - _SubRate)":"";
		$strComm = ($isRealShow == 1)?"*_MemCommission":"*0";
		$strWagerType = "0,2";
		$strSWType = "2";
		break;
}

if($strBT == "14"){
	$strMulti = str_replace('_', 'b.', $strMulti);
	$strComm = str_replace('_', 'b.', $strComm);
	$strSQL = "select a.Bet_Type as Bet_Type, a.Number as Number, Count(*) as total, sum(IF(b.WagerType = ".$strSWType.",b.MemStake*-1,b.MemStake".$strMulti.")*b.MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",b.MemPayOff*-1,b.MemPayOff".$strMulti.")*b.MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",b.MemStake*-1,b.MemStake".$strMulti.")*b.MemExchangeRate".$strComm.") as commstatus  from `bet_ParlaysNumber` a, `bet_BetListTmp` b where a.BetListID = b.ID and b.GambleID = ".$intGID.$strSeach2." group by a.Bet_Type, a.Number";

}elseif(preg_match("/15|16|17|18|19|20|21|22|23|24|25|26|27|28|30|31|32|33|34|35/", $strBT)){ //連碼 不中
	$strMulti= str_replace('_', 'b.', $strMulti);
	$strComm = str_replace('_', 'b.', $strComm);
	$strSQL = "select a.Bet_Type as Bet_Type, a.Number as Number, a.Ball1, a.Ball2, a.Ball3, a.Ball4, a.Ball5, Count(*) as total, sum(IF(b.WagerType = ".$strSWType.",a.MemStake*-1,a.MemStake".$strMulti.")*b.MemExchangeRate) as betstatus, sum(IF(b.WagerType = ".$strSWType.",a.MemPayOff*-1,a.MemPayOff".$strMulti.")*b.MemExchangeRate) as paystatus,  sum(IF(b.WagerType = ".$strSWType.",a.MemStake*-1,a.MemStake".$strMulti.")*b.MemExchangeRate".$strComm.") as commstatus  from `bet_ParlaysNumberEven` a, `bet_BetListTmp` b where a.BetListID = b.ID and b.GambleID = ".$intGID.$strSeach2." and  b.Bet_Type in (".$strBT.") and b.isPayOff >= 0 group by a.Bet_Type, a.Number";

/*}elseif(preg_match("/30/", $strBT)){ //尾數連
	$strMulti= str_replace('_', 'b.', $strMulti);
	$strComm = str_replace('_', 'b.', $strComm);
	$strSQL = "select a.Bet_Type as Bet_Type, a.Number as Number, Count(*) as total, sum(IF(b.WagerType = ".$strSWType.",b.MemStake*-1,b.MemStake".$strMulti.")*b.MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",b.MemPayOff*-1,b.MemPayOff".$strMulti.")*b.MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",b.MemStake*-1,b.MemStake".$strMulti.")*b.MemExchangeRate".$strComm.") as commstatus  from `bet_ParlaysNumber` a, `bet_BetListTmp` b where a.BetListID = b.ID and b.GambleID = ".$intGID.$strSeach2." and  b.Bet_Type in (".$strBT.") and b.isPayOff >= 0 group by a.Bet_Type, a.Number";
*/
}else{
	$strMulti = str_replace('_', '', $strMulti);
	$strComm = str_replace('_', '', $strComm);
	$strSQL = "select Bet_Type, Number, Count(*) as total, sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus from `bet_BetListTmp` where Bet_Type in (".implode(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 and WagerType in (".$strWagerType.") group by Bet_Type, Number";
} //if($strBT == "14")//
//print $strSQL;
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Wager Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("LoginError", "", 1);
}

$arrGroupCounter = array(15 => 1, 16 => 2, 17 => 3, 18 => 3, 19 => 4, 20 => 4, 21 => 5, 22 => 5, 23 => 5, 24 => 2, 25 => 2, 26 => 3, 27 => 4, 28 => 5, 30 => 2, 31 => 3, 32 => 4, 33 => 2, 34 => 3, 35 => 4);
while($Rows = PMA_mysql_fetch_object($RS)){
	if(preg_match("/15|16|17|18|19|20|21|22|23|24|25|26|27|28|30|31|32|33|34|35/", $Rows -> Bet_Type)){
		$intGroupCounter = $arrGroupCounter[$Rows -> Bet_Type];
		for($i = 1; $i <= $intGroupCounter; $i++){
			$strBall = "Ball".$i;
			$intNum = $Rows -> $strBall;
			$arrWCount[$intGID][$Rows -> Bet_Type][$intNum] += $Rows -> total;// / $intGroupCounter;
			$arrWSum[$intGID][$Rows -> Bet_Type][$intNum] += $Rows -> betstatus / $intGroupCounter;
			$arrPayOff[$intGID][$Rows -> Bet_Type][$intNum] += $Rows -> paystatus / $intGroupCounter;
			$arrCommission[$intGID][$Rows -> Bet_Type][$intNum] += $Rows -> commstatus / $intGroupCounter;
		}
	}else{
		$arrWCount[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> total;
		$arrWSum[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> betstatus;
		$arrPayOff[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> paystatus;
		$arrCommission[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> commstatus;
	}
}

$intTmpBT = 0;
$arrSumStake = array();
$arrSumPayOff = array();
$arrSumCommission = array();

if(is_array($arrTodayGame)){
	reset($arrTodayGame);
	foreach($arrTodayGame as $intBT => $arrN){
		foreach($arrN as $intNum => $Rows){
			$intWCount = $arrWCount[$Rows["GambleID"]][$intBT][$Rows["Number"]] + 0;
			$intWSum = $arrWSum[$Rows["GambleID"]][$intBT][$Rows["Number"]] + 0;
			$intPayOff = $arrPayOff[$Rows["GambleID"]][$intBT][$Rows["Number"]] + 0;
			$intCommission = $arrCommission[$Rows["GambleID"]][$intBT][$Rows["Number"]] + 0;


			$intApointNum = $_arrGLineNumToApointNum[$Rows["LottoID"]][$Rows["Bet_Type"]][$Rows["Number"]];
			if($intApointNum == null)$intApointNum = 0;
			$flGLine = $_arrGambleType[$Rows["Bet_Type"]][$intApointNum];

			$strCon .= "parent.Game".$Rows["Bet_Type"]."[".$Rows["Number"]."]=new Array('".$Rows["LottoID"]."','".$Rows["GambleID"]."','".$Rows["Bet_Type"]."','".$Rows["Number"]."','".$Rows["OddsID"]."','".($Rows["Odds"] + $flGLine)."','".$Rows["isStopBet"]."','".$intWCount."','".$intWSum."','".$intPayOff."','".$intCommission."','".$intSelfStop."');\n";

			switch($Rows["Bet_Type"]){
				case 11:
				case 12:
				case 13:
					$intNum = floor($Rows["Number"] / 10);
					$arrSumStake[$Rows["Bet_Type"]][$intNum] += $intWSum;
					$arrSumPayOff[$Rows["Bet_Type"]][$intNum] += $intPayOff;
					$arrSumCommission[$Rows["Bet_Type"]][$intNum] += $intCommission;
					break;

				default:
					$arrSumStake[$Rows["Bet_Type"]] += $intWSum;
					$arrSumPayOff[$Rows["Bet_Type"]] += $intPayOff;
					$arrSumCommission[$Rows["Bet_Type"]] += $intCommission;
					break;

			}
		}
	}
}

/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
<?php 
print $strCon."\n";

foreach($arrSumStake as $intBT => $val){
	if(is_array($val)){
		foreach($val as $intN => $val2){
			print "parent.sumStake[".$intBT."][".$intN."] = ".round($val2).";\n";
			print "parent.sumPayOff[".$intBT."][".$intN."] = ".round($arrSumPayOff[$intBT][$intN]).";\n";
			print "parent.sumCommission[".$intBT."][".$intN."] = ".round($arrSumCommission[$intBT][$intN]).";\n";
		}
	}else{
		print "parent.sumStake[".$intBT."] = ".round($arrSumStake[$intBT]).";\n";
		print "parent.sumPayOff[".$intBT."] = ".round($arrSumPayOff[$intBT]).";\n";
		print "parent.sumCommission[".$intBT."] = ".round($arrSumCommission[$intBT]).";\n";
	}
}

switch($strBT){
	case "1":
	case "2":
	case "3":
	case "4":
	case "1,2,3,4":
		print "parent.ShowNumberOdds(1);\n";
		break;

	case "15":
	case "16":
	case "17":
	case "18":
	case "19":
	case "20":
	case "21":
	case "22":
	case "23":
	case "24":
		print "parent.ShowJoinNumberOdds(".$strBT.");\n";
		break;

	case "9":
	case "10":
	case "11":
	case "12":
	case "13":
	case "9,10,11,12,13":
		print "parent.ShowNormalNumber1_4Odds(0);\n";
		break;

	case "14":
		print "parent.ShowNormalNumber1_4Odds(1);\n";
		break;

	case "29":
		print "parent.ShowMantissaOdds();\n";
		break;

	case "5":
	case "6":
	case "7":
	case "8":
		print "parent.ShowNumberOdds(".$strBT.");\n";
		break;

	case "25":
	case "26":
	case "27":
	case "28":
		print "parent.ShowFiveNotWinNumber(".$strBT.");\n";
		break;

	case "30":
	case "31":
	case "32":
	case "33":
	case "34":
	case "35":
	case "30,31,32,33,34,35":
		print "parent.ShowBT3035Number(30);\n";
		break;
}
?>
//-->
</script>